package com.youlv.yl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlv.yl.dao.MoodForwardDao;
import com.youlv.yl.dto.MoodFDto;
import com.youlv.yl.dto.UserMoodDto;
import com.youlv.yl.entity.mood.MoodForward;
import com.youlv.yl.entity.mood.UserMood;
import com.youlv.yl.dao.UserMoodDao;
import com.youlv.yl.service.intf.UserMoodService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlv.yl.util.BaiduContentUtils;
import com.youlv.yl.util.StringUtils;
import com.youlv.yl.util.TokenUtils;
import com.youlv.yl.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fenghe
 * @since 2022-01-15
 */
@Service
public class UserMoodServiceImpl extends ServiceImpl<UserMoodDao, UserMood> implements UserMoodService {
    @Autowired
    private UserMoodDao dao;
    @Autowired
    MoodForwardDao forwardDao;
    @Override
    public R save(UserMoodDto dto) {
        // 1.入参校验
        if(dto != null && StringUtils.isNotEmpty(dto.getContent())) {
            // 2.文本审核，防止出现违规内容
            if(BaiduContentUtils.textCensor(dto.getContent())) {
                // 3.创建对象，实现新增
                UserMood userMood = new UserMood();
                userMood.setContent(dto.getContent());
                userMood.setType(dto.getType());
                userMood.setUid(dto.getUid());
                userMood.setCtime(new Date());
                userMood.setParentid(dto.getParentid());
                dao.insert(userMood);
               // 4.如果是有带图片或视频的，需要新增到媒体表
                return R.ok();
            }
        }
        return R.fail("亲，请按照要求传递参数！");
    }

    @Override
    public R queryMy(Long uid) {
        // 1.入参校验
        if(uid > 0) {
            // 2.构建查询条件对象
            QueryWrapper<UserMood> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uid", uid);
            queryWrapper.orderByDesc("ctime");
            // 3.返回结果
            return R.ok(dao.selectList(queryWrapper));
        }
        return R.fail("亲，请按照要求传递参数");
    }

    @Override
    public R queryPage(int page, int limit) {
        // 1.验证参数
        if(page > 0 && limit > 0) {
            // 2.创建分页对象
            Page<UserMood> moodPage = new Page<>(page, limit);
            // 3.构建查询对象，实现排序
            QueryWrapper<UserMood> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("ctime");
            // 4.返回查询结果
            return R.ok(dao.selectPage(moodPage, queryWrapper));
        }
        return R.fail("请传递分页参数");
    }

    @Override
    public R queryDetail(Long id) {
        // 1.入参校验
            if (id > 0) {
                // 2.查询获取的详情
                return R.ok(dao.selectDetail(id));
            }
            return R.fail("请传递参数！");
        }

    @Override
    public R saveForward(MoodFDto dto) {
        // 1.入参校验
        if(dto != null && dto.getToken().length() > 0 && dto.getUmid() > 0) {
            // 通过token获取用户id
            Long uid = TokenUtils.getUidByToken(dto.getToken());
            // 2.新增动态
            UserMood userMood = new UserMood();
            userMood.setParentid(dto.getUmid());
            userMood.setUid(uid);
            userMood.setType(4);
            userMood.setContent("转发动态");
            userMood.setCtime(new Date());
            dao.insert(userMood);
            // 3.新增转发记录
            MoodForward forward = new MoodForward();
            forward.setUid(uid);
            forward.setFumid(dto.getUmid());
            forward.setTumid(userMood.getId());
            forward.setCtime(new Date());
            forwardDao.insert(forward);
            // 4.返回结果
            return R.ok();
        }
        return R.fail("请检查参数！");
    }

    @Override
    public R queryF(int umid) {
        // 1.校验
        if(umid > 0) {
            // 2.创建查询条件对象
            QueryWrapper<MoodForward> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("fumid", umid);
            queryWrapper.orderByDesc("ctime");
            // 3.返回
            return R.ok(forwardDao.selectList(queryWrapper));
        }
        return R.fail("请检查参数！");
    }
}
